package com.mbg17.utils;

import java.sql.*;

/**
 * @author LuYuan
 * @version 1.0
 * @date 2022/2/4 7:30 下午
 * @File : JDBCUtil.java
 * @Describe: IntelliJ IDEA
 */
public class JDBCUtil {

    /**
     * 通过静态代码块赋值
     */

    public static Connection getConnection(String url, String user, String password, String driver) throws SQLException {
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            return null;
        }
    }

    public static void close(Statement st, Connection conn) {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet res, Statement st, Connection conn) {
        if (res != null) {
            try {
                res.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
